﻿using BlawkayEngine;
using BlawkayEngine.Input;
using BlawkayEngine.ScreenSystem;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace BlawkayHockey
{
    public abstract class HomeMenuScreen : MenuScreen
    {
        public HomeMenuScreen(Game oGame, string oTitle)
            : base(oGame, oTitle)
        {
            _BackgroundMenuOption = GameInfo.Texture_Square;
            _FontMenuOption = oGame.Content.Load<SpriteFont>("Menu\\MenuOption");

            TransitionRate *= 8f;
        }

        public override void Update(ScreenManager oManager)
        {
            KeyboardInput oInput = GameInfo.KeyboardInput;

            if (oInput.JustPressed(Keys.Up))
            {
                IndexDown();
            }
            if (oInput.JustPressed(Keys.Down))
            {
                IndexUp();
            }

            if (Active)
            {
                if (oInput.JustPressed(Keys.Space))
                {
                    Select(oManager);
                }
                if (oInput.JustPressed(Keys.LeftShift, Keys.RightShift))
                {
                    Cancel(oManager);
                }
            }
        }

        public override void Draw(ScreenManager oManager, SpriteBatch oSB)
        {
            Rectangle oBackgroundRect = new Rectangle(100, 300, 400, 1);
            Vector2 oOrigin = Vector2.Zero;
            SpriteEffects oEffect = SpriteEffects.None;
            float oScale = 1.0f;
            float oRotation = 0.0f;

            int oIndex = 0;
            foreach (string oOption in Options)
            {
                Color oOptionColor = Index == oIndex ?
                    Color.Yellow : Color.White;

                Vector2 oMeasure = _FontMenuOption.MeasureString(oOption);
                oBackgroundRect.Height = (int)oMeasure.Y + 6;
                Vector2 oOptionPosition = new Vector2(oBackgroundRect.Left + 5, oBackgroundRect.Top + 3);

                oSB.Draw(_BackgroundMenuOption, oBackgroundRect, null, Color.Black * 0.25f * Transition, oRotation, oOrigin, oEffect, 0.5f);
                oSB.DrawString(_FontMenuOption, oOption, oOptionPosition, oOptionColor * Transition, oRotation, oOrigin, oScale, oEffect, 1.0f);

                oBackgroundRect.Y = oBackgroundRect.Bottom + 5;
                oIndex++;
            }
        }

        protected void GoToDestination(HomeDestination oDestination, ScreenManager oManager, object oTag = null)
        {
            _Destination = oDestination;
            _DestinationTag = oTag;

            switch (_Destination)
            {
                case HomeDestination.CreateMatch:
                    GameInfo.ScreenManager = ScreenManager.SendScreensToNewManager(oManager, new XboxLiveManager(oManager.Game));
                    GameInfo.ScreenManager.AddScreen(new CreateMatchScreen(oManager.Game));
                    break;

                case HomeDestination.QuickSearch:
                    Networking.FindSessions();
                    GameInfo.ScreenManager = ScreenManager.SendScreensToNewManager(oManager, new XboxLiveManager(oManager.Game));
                    GameInfo.ScreenManager.AddScreen(new SearchResultsScreen(oManager.Game));
                    break;

                case HomeDestination.Main:
                case HomeDestination.PlayNow:
                case HomeDestination.XboxLive:
                case HomeDestination.OptionsMenu:
                case HomeDestination.OptionsSettings:
                default: oManager.RemoveScreen(this); break;
            }
        }

        public override void ActionWhenFullyAdded(ScreenManager oManager)
        {
            ////////////
        }

        public override void ActionWhenFullyRemoved(ScreenManager oManager)
        {
            Screen oDestinationScreen = null;

            switch (_Destination)
            {
                case HomeDestination.Main:
                    oDestinationScreen = new MainMenuScreen(oManager.Game);
                    break;
                case HomeDestination.PlayNow:
                    ////////////
                    break;
                case HomeDestination.XboxLive:
                    oDestinationScreen = new XboxLiveMenuScreen(oManager.Game);
                    break;
                case HomeDestination.OptionsMenu:
                    oDestinationScreen = new OptionsMenuScreen(oManager.Game);
                    break;
                case HomeDestination.OptionsSettings:
                    if (_DestinationTag is OptionsSettingsScreen.Section)
                        oDestinationScreen = new OptionsSettingsScreen(oManager.Game, (OptionsSettingsScreen.Section)_DestinationTag);
                    else
                        oDestinationScreen = new OptionsSettingsScreen(oManager.Game);
                    break;

                case HomeDestination.CreateMatch:
                case HomeDestination.QuickSearch:
                case HomeDestination.CustomSearch:
                    // Intentionally left blank
                    // Do same for other manager destinations
                    break;

                default:
                    oDestinationScreen = new MainMenuScreen(oManager.Game);
                    break;
            }

            if (oDestinationScreen != null)
                oManager.AddScreen(oDestinationScreen);
        }

        private HomeDestination _Destination;
        private object _DestinationTag;

        private Texture2D _BackgroundMenuOption;
        private SpriteFont _FontMenuOption;

        public enum HomeDestination
        {
            Main,
            PlayNow,
            XboxLive,
            OptionsMenu,
            OptionsSettings,
            CreateMatch,
            QuickSearch,
            CustomSearch
        }

    }
}
